//
//  BBSTableViewController.m
//  CarHome
//
//  Created by lanou3g on 15-5-2.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BBSTableViewController.h"
#import "BBS.h"
#import "BBSInfo.h"
#import "BBSTableViewCell.h"

@interface BBSTableViewController ()

@property (nonatomic, retain) BBS *bbs;
@property (nonatomic, retain) BBSInfo *bbsInfo;
@property (nonatomic, retain) NSMutableArray *list0;
@property (nonatomic, retain) NSMutableArray *list1;
@property (nonatomic, retain) NSMutableArray *list2;
@property (nonatomic, retain) NSMutableArray *list3;
@property (nonatomic, retain) NSMutableArray *list;
@property (nonatomic, retain) NSMutableArray *urls;
@property (nonatomic, retain) UISegmentedControl *segmentedControl;

@end

@implementation BBSTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"BBSTableViewCell" bundle:nil] forCellReuseIdentifier:@"BBSTableViewCellIdentifier"];
    [self addSegmentedControl];
}


- (void)viewWillAppear:(BOOL)animated
{
    
    [self.tableView reloadData];
}



- (void)addSegmentedControl {
    self.segmentedControl = [[[UISegmentedControl alloc] initWithItems:@[@"最后回复", @"最新发布", @"精华帖", @"质量反馈"]] autorelease];
    _segmentedControl.frame = CGRectMake(20, 20, kWIDTH - 40, 30);
    _segmentedControl.tintColor = [UIColor blueColor];
    _segmentedControl.selectedSegmentIndex = 0;
    [_segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, 50)] autorelease];
    [view addSubview:_segmentedControl];
    self.tableView.tableHeaderView = view;
    
    self.list0 = [NSMutableArray array];
    self.list1 = [NSMutableArray array];
    self.list2 = [NSMutableArray array];
    self.list3 = [NSMutableArray array];
    self.list = [[NSMutableArray alloc] initWithObjects:_list0, _list1, _list2, _list3, nil];
    self.urls = [[NSMutableArray alloc] initWithObjects:BBSURLwithCarID(_seriesID), BBSURLwithCarID2(_seriesID), BBSURLwithCarID3(_seriesID), BBSURLwithCarID4(_seriesID), nil];
}

- (void)loadData {
    if (_list0.count == 0) {
        [self loadDataWithIndex:0];
    } else {
        [self.tableView reloadData];
    }
}

- (void)loadDataWithIndex:(NSInteger)selectedIndex {
    
    __block typeof(self) weakSelf = self;
    if ([_list[selectedIndex] count] == 0) {
        NSLog(@"%@", _urls[selectedIndex]);
        
        [HTTPRequest requestWithURL:_urls[selectedIndex] method:@"GET" result:^(NSData *data) {
            NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            self.bbs = [[BBS new] autorelease];
            [_bbs setValuesForKeysWithDictionary:dict[@"result"]];
            
            _list[selectedIndex] = [[NSMutableArray alloc]initWithArray:_bbs.list];
            
            [weakSelf.tableView reloadData];
        }];

    } else {
        [self.tableView reloadData];
    }
    
}

- (void)segmentedControlAction:(UISegmentedControl *)sender {
    [self loadDataWithIndex:sender.selectedSegmentIndex];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_list[_segmentedControl.selectedSegmentIndex] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BBSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BBSTableViewCellIdentifier" forIndexPath:indexPath];
    
    BBSInfo *bbsi = _list[_segmentedControl.selectedSegmentIndex][indexPath.row];
    
    cell.title.text = bbsi.title;
    cell.lastreplydate.text = [NSString stringWithFormat:@"回复: %@", bbsi.lastreplydate];
    cell.postusername.text = bbsi.postusername;
    cell.replycounts.text = [NSString stringWithFormat:@"%@回", bbsi.replycounts];
    if ([bbsi.topictype isEqualToString:@"图"]) {
        cell.image.image = [UIImage imageNamed:@"bg_marrow2"];
    } else if ([bbsi.topictype isEqualToString:@"精"]) {
        cell.image2.image = [UIImage imageNamed:@"bg_marrow"];
    } else if ([bbsi.topictype isEqualToString:@"顶"]) {
        cell.image2.image = [UIImage imageNamed:@"bg_fulllevel"];
    } else {
        cell.image.image = nil;
        cell.image2.image = nil;
    }
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
